package com.rjwh.dingdong.client.mvp.presenter.impl;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;

import com.aishua.lib.task.AsResult;
import com.rjwh.dingdong.client.bean.jsonbean.ResGetDingDongMathGameBean;
import com.rjwh.dingdong.client.bean.localbean.DrawBookMainPageTopic;
import com.rjwh.dingdong.client.bean.localbean.GetDingDongMathGameBean;
import com.rjwh.dingdong.client.bean.localbean.MathBookMenuData;
import com.rjwh.dingdong.client.bean.localbean.MathGameHomeGameItemData;
import com.rjwh.dingdong.client.bean.localbean.MathGameMathBook;
import com.rjwh.dingdong.client.constant.NetConstant;
import com.rjwh.dingdong.client.mvp.BasePresenter;
import com.rjwh.dingdong.client.mvp.model.impl.MathGameDetailModel;
import com.rjwh.dingdong.client.mvp.model.impl.MathGameModel;
import com.rjwh.dingdong.client.mvp.presenter.IMathGamePresenter;
import com.rjwh.dingdong.client.mvp.view.MathGameView;
import com.rjwh.dingdong.client.service.PlaybackService;

import java.util.List;

/**
 * @author Kay
 * @ClassName MathGamePresenter
 * @Description
 * @date 2016-7-5
 * @history 2016-7-5 author: description:
 */
public class MathGamePresenter extends BasePresenter<MathGameView> implements IMathGamePresenter {

    private MathGameModel model;
    private PlaybackService mPlaybackService;
    private boolean mIsServiceBound;

    public MathGamePresenter(Context context, MathGameView mMvpView) {
        super(context, mMvpView);
        model = new MathGameModel(this);
    }

    /**
     * 获取数学主页
     */
    @Override
    public void doGetMathGameMainPage(String bookid) {
        if (isViewAttached()) {
//            getMvpView().hideErrorView();
//            getMvpView().showLoadingView();
            model.doGetDingDongMathGame(bookid);
        }
    }

    /**
     * 请求结果
     *
     * @param businessType
     * @param mAsResult
     * @param isRefresh
     */
    @Override
    public void onServerDataAcquired(int businessType, AsResult mAsResult, boolean isRefresh) {
        if (getMvpView() != null) {
            getMvpView().hideLoadingView();
            switch (businessType) {
                case NetConstant.GETDINGDONGMATHGAME:
                    getMvpView().setRefreshing(false);
                    ResGetDingDongMathGameBean resGetDingDongMathGameBean = (ResGetDingDongMathGameBean) mAsResult.getObj();
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS && resGetDingDongMathGameBean != null && resGetDingDongMathGameBean.getData() != null) {
                        getMvpView().hideErrorView();
                        GetDingDongMathGameBean data = resGetDingDongMathGameBean.getData();
                        MathGameMathBook mathBook = data.getMathBook();
                        List<MathBookMenuData> bookmenulist = data.getBookmenulist();
                        List<MathGameHomeGameItemData> footsteplist = data.getFootsteplist();
                        List<MathGameHomeGameItemData> goodgamelist = data.getGoodgamelist();
                        List<DrawBookMainPageTopic> topiclist = data.getTopiclist();
                        if (mathBook != null && bookmenulist != null && footsteplist != null && goodgamelist != null && topiclist != null) {
                            getMvpView().LoadData(mathBook, bookmenulist, footsteplist, goodgamelist, topiclist);
                        } else {
                            getMvpView().showErrorView();
                        }
                    } else {
                        getMvpView().showErrorView();
                    }
                    break;
                default:
                    break;
            }
        }
    }

    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            mPlaybackService = ((PlaybackService.LocalBinder) service).getService();
            getMvpView().onPlaybackServiceBound(mPlaybackService);
        }

        public void onServiceDisconnected(ComponentName className) {
            mPlaybackService = null;
            getMvpView().onPlaybackServiceUnbound();
        }
    };

    private void bindPlaybackService() {
        getContext().bindService(new Intent(getContext(), PlaybackService.class), mConnection, Context.BIND_AUTO_CREATE);
        mIsServiceBound = true;
    }

    private void unbindPlaybackService() {
        if (mIsServiceBound) {
            // Detach our existing connection.
            getContext().unbindService(mConnection);
            mIsServiceBound = false;
        }
    }

    public void subscribe() {
        bindPlaybackService();
    }
    @Override
    public void detachView() {
        unbindPlaybackService();
        model.detachView();
        detachViews();
    }

}
